package com.yidu.ruifeng_logistics.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.ruifeng_logistics.entity.LogisticsInStorage;
import com.yidu.ruifeng_logistics.entity.LogisticsStorage;
import com.yidu.ruifeng_logistics.entity.LogisticsWaybill;
import com.yidu.ruifeng_logistics.service.LogisticsInStorageService;
import com.yidu.ruifeng_logistics.service.LogisticsStorageService;
import com.yidu.ruifeng_logistics.utils.BaseMessage;
import com.yidu.ruifeng_logistics.vo.LogisticsInStorageVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 入库表(LogisticsInStorage)表控制层
 *
 * @author 肖楚科
 * @since 2021-03-15 10:57:54
 */
@RestController
@RequestMapping("logisticsInStorage")
public class LogisticsInStorageController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsInStorageService logisticsInStorageService;

    @Resource
    private LogisticsStorageService logisticsStorageService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("/showUpdate")
    public Map<String, Object> selectOne(Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("data",this.logisticsInStorageService.queryById(id));
        //storageName为根据id查出的入库信息的仓库id，然后根据仓库id查处的仓库的仓库名
        map.put("storageName",logisticsStorageService.queryById
                (this.logisticsInStorageService.queryById(id).getStorageId()).getStorageName());
        return map;
    }

    /**
     * 根据条件查询入库情况
     * @param logisticsInStorageVo 仓库对象
     * @return 信息对象
     */
    @ApiOperation(value = "根据条件查询入库情况",notes = "返回状态信息")
    @GetMapping("/queryAll")
    public Map<String, Object> queryAll(LogisticsInStorageVo logisticsInStorageVo, int page, int limit){
        //将参数传入到分页助手
        PageHelper.startPage(page,limit);
        List<LogisticsInStorageVo> list = logisticsInStorageService.queryAll(logisticsInStorageVo);
        Map<String, Object> map = new HashMap<>();

        PageInfo<LogisticsInStorageVo> pageInfo = new PageInfo<>(list);

        map.put("code",0);
        map.put("msg","查询成功");
        map.put("data",pageInfo.getList());//可以直接写list
        map.put("count",pageInfo.getTotal()); //总行数
        return map;
    }

    /**
     * 根据id删除入库情况
     * @param id id参数
     * @return 状态信息
     */
    @ApiOperation(value = "根据id删除入库情况",notes = "返回状态信息")
    @PostMapping("/delete")
    public BaseMessage delete(int id){
        return logisticsInStorageService.deleteById(id)?BaseMessage.success():BaseMessage.fail();
    }

    /**
     * 批量删除方法
     * @param ids id拼接字符串
     * @return 状态信息
     */
    @ApiOperation(value = "批量删除方法",notes = "返回状态信息")
    @PostMapping("/deleteAll")
    public BaseMessage deleteAll(String ids){
        return logisticsInStorageService.deleteAll(ids)>0?BaseMessage.success():BaseMessage.fail();
    }

    /**
     * 根据id新增或者修改入库信息
     * @param logisticsInStorageVo 入库对象
     * @return 状态信息
     */
    @ApiOperation(value = "根据id新增或者修改入库信息",notes = "返回状态信息")
    @PostMapping("/addOrUpdate")
    public BaseMessage addOrUpdate(LogisticsInStorageVo logisticsInStorageVo){
        if (StringUtils.isEmpty(logisticsInStorageVo.getInStorageId())){
            logisticsInStorageService.insert(logisticsInStorageVo);
        }else {
            logisticsInStorageService.update(logisticsInStorageVo);
        }
        return BaseMessage.success();
    }

}
